package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Slf4j
@Component
public class Template34 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void table(XSLFShape shape, JsonNode data) {
        TableShape tableShape = (TableShape)shape;
        int numberOfColumns = tableShape.getNumberOfColumns();
        int numberOfRows = tableShape.getNumberOfRows();

        List<TempBeen> beenList = getTempBeens(data);
        List<String> names = getNameList(beenList);
        List<String> dataList = getDataList();

        //遍历获取每一个单元格
        for (int row = 0; row<numberOfRows; row++ ){
            for (int col =0 ;col < numberOfColumns; col++){
                TableCell cell = tableShape.getCell(row, col);
                if (cell.getTextParagraphs().size() > 0){
                    XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                    if (cellParagraphs.getTextRuns().size()> 0){

                        XSLFTextRun textRun = cellParagraphs.getTextRuns().get(0);
                        if (row == 0){
                            if (col == 0) {
                                continue;
                            }
                            textRun.setText(dataList.get(col-1));
                            continue;
                        }
                        if (col == 0){
                            textRun.setText(names.get(row-1));
                            continue;
                        }
                        textRun.setText(getCellData(names.get(row-1),dataList.get(col-1),beenList));
                    }
                }
            }
        }
    }

    private String getCellData(String name,String dataName,List<TempBeen> list) {
        List<TempBeen> collect = list.stream().filter(been -> been.getTempName().equals(name) && been.getTempData().equals(dataName)).collect(toList());
        return StringUtil.decimalFormat(collect.get(0).getTempCount());
    }


    public List<String> getNameList(List<TempBeen> list){
        List<TempBeen> name1s = list.stream().filter(been -> been.getTempData().equals("人次总计"))
                .sorted(Comparator.comparing(TempBeen::getTempCount).reversed())
                .collect(toList());
        List<String> names = new ArrayList<>();
        List<String> names1 = new ArrayList<>();
        for (TempBeen name1 : name1s) {
            if (name1.getTempName().equals("每日签到") || name1.getTempName().equals("运动打卡")
                    || name1.getTempName().equals("运动步数换积分") || name1.getTempName().equals("阅读文章并通知好友")){
                names.add(name1.getTempName());
                continue;
            }
            if (name1.getTempName().equals("人数总计")){
                continue;
            }
            names1.add(name1.getTempName());
        }
        names.addAll(names1);
        names.add("人数总计");
        System.out.println("name:"+JsonUtil.serialize(names));
        return names;
    }

    private List<TempBeen> getTempBeens(JsonNode data) {
        JsonNode jsonNode = data.get("beens");
        List<TempBeen> beens = new ArrayList<>();
        for (JsonNode node : jsonNode) {
            TempBeen tempBeen = new TempBeen();
            tempBeen.setTempName(node.get("tempName").asText());
            tempBeen.setTempCount(node.get("tempCount").asLong());
            tempBeen.setTempData(node.get("tempData").asText());
            beens.add(tempBeen);
        }
        return beens;
    }

    public List<String> getDataList(){
        List<String> dateValue = DateUtil.getDateValue().stream().sorted(Comparator.comparing(String::trim)).collect(toList());
        dateValue.add("人次总计");
        return dateValue;
    }

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("28KEY-1")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("28KEY-3")){
                        String dataPeriod = data.get("dataPeriod").asText();
                        textRun.setText(dataPeriod);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text Exception:{}",34,e);
                }
            }
        }
    }
}
